﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Xeml.Document;
using System.Threading;
using Xeml.Document.Contracts;
using CompilerControls.Properties;
using Xeml.Sampling.Contracts;

namespace CompilerControls
{


    public partial class ProviderCard : UserControl
    {
        private DataProviderResource _provider;

        public DataProviderResource DataProvider
        {
            get { return _provider; }
            set { _provider = value; Setup(); }
        }

        private void Setup()
        {
            if (_provider != null)
            {
                _provider.LoadComponent(_provider.Uri);
                // customPropDisplayCtrl1.Setup(_provider.Provider);
                kryptonLabelName.Text = _provider.FriendlyName;
                kryptonTextBox1.Text = _provider.FriendlyName;


                if (_provider.ComponentState == ComponentLoadState.Loaded && _provider.Provider.Logo != null)
                    pictureBox.Image = _provider.Provider.Logo;

                if (_provider.ComponentState == ComponentLoadState.Loaded)
                {
                    kryptonLabelAutoMap.Text = _provider.Provider.SupportsAutoMapping ? "Yes" : "No";
                    kryptonLabelAutoMap.Text = _provider.Provider is IWriteableProvider ? "Yes" : "No";
                }
                else if (_provider.ComponentState == ComponentLoadState.Failed)
                {
                    kryptonLabelAutoMap.Text = "--";
                    kryptonLabelInit.Text = "--";
                    kryptonLabelStatus.Text = "Load failed";
                }
                else if (_provider.ComponentState == ComponentLoadState.UnknownComponent)
                {
                    kryptonLabelAutoMap.Text = "--";
                    kryptonLabelInit.Text = "--";
                    kryptonLabelStatus.Text = "Unknown Provider";
                }
            }
        }

        private Color _originalBorderColor;
        public ProviderCard()
        {
            InitializeComponent();
            _originalBorderColor = kryptonHeaderGroupFront.StateCommon.Border.Color1;
        }

        private void kryptonLinkLabel1_LinkClicked(object sender, EventArgs e)
        {
            SelectMe();
        }

        private bool SaveName()
        {
            if (kryptonTextBox1.Text != _provider.FriendlyName)
            {
                IDocument xemlDoc = (_provider.Collection.Owner as DocumentResources).XemlDocument;
                if (xemlDoc.DocumentResources.DataProvider.Contains(kryptonTextBox1.Text))
                {
                    MessageBox.Show("A provider with this name already exits!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    kryptonTextBox1.SelectAll();
                    kryptonTextBox1.Focus();
                    return false;
                }
                else if (string.IsNullOrEmpty(kryptonTextBox1.Text))
                {
                    MessageBox.Show("A provider name can't be empty!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    kryptonTextBox1.SelectAll();
                    kryptonTextBox1.Focus();
                    return false;
                }
                else
                {
                    if (xemlDoc.ProviderDataMappings.ContainsKey(_provider.FriendlyName))
                    {
                        var item = xemlDoc.ProviderDataMappings[_provider.FriendlyName];
                        xemlDoc.ProviderDataMappings.Add(kryptonTextBox1.Text, item);
                        xemlDoc.ProviderDataMappings.Remove(_provider.FriendlyName);
                    }
                    _provider.FriendlyName = kryptonTextBox1.Text;
                    kryptonLabelName.Text = _provider.FriendlyName;
                    return true;
                }
            }
            else
            {
                return true;
            }
        }

        public event EventHandler Selected;

        private void kryptonLabelName_Click(object sender, EventArgs e)
        {
            SelectMe();
        }

        public void SelectMe()
        {
            if (Selected != null)
                Selected(this, new EventArgs());
            kryptonHeaderGroupFront.StateCommon.Border.Color1 = Color.MediumBlue;

        }

        public void DeSelect()
        {
            kryptonHeaderGroupFront.StateCommon.Border.Color1 = _originalBorderColor;
        }

        private void kryptonLinkLabel4_LinkClicked(object sender, EventArgs e)
        {
            if (MessageBox.Show("You about to delete this provider and all related mappings!\r\nDo you want proceed?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                IDocument xemlDoc = (_provider.Collection.Owner as DocumentResources).XemlDocument;
                List<KeyValuePair<string, DataMappings>> toRem = new List<KeyValuePair<string, DataMappings>>();
                xemlDoc.ProviderDataMappings.Remove(_provider.FriendlyName);
                xemlDoc.DocumentResources.DataProvider.Remove(_provider);
            }
        }


        private bool editMode = false;
        private void kryptonLinkLabel3_LinkClicked(object sender, EventArgs e)
        {
            SelectMe();
            if (editMode)
            {
                if (SaveName())
                    editMode = !editMode;
            }
            else
            {
                editMode = !editMode;

            }

            kryptonTextBox1.Visible = editMode;
            kryptonLinkLabelEdit.Text = editMode ? "Save" : "Edit";
            kryptonLinkLabelEdit.Values.Image = editMode ? Resources.saveHS : Resources.EditInformationHS;
            if (editMode)
                kryptonTextBox1.Focus();
            //this.Height = 150;
            //this.Width -= 50;
            //flipPanel1.Flip();
        }


    }
}
